//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.IO;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Blogs.Controls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;

namespace CommunityServer.ControlPanel.BlogAdmin
{
	/// <summary>
	/// Summary description for SkinOptionsPage.
	/// </summary>
	public class SkinOptionsPage : BaseBlogAdminPage
	{

		#region Child Controls
		
		protected CheckBox enableThemes;
		protected DropDownList defaultTheme;
        protected ResourceLinkButton SaveButton;
		protected TextBox aggregatePostSize;
		protected ConfigOKStatusMessage Status;
        protected FileOnlyStatusMessage FOStatus;
		protected DropDownList Language;
		protected DropDownList baseTheme;
		protected TextBox aggregatePostCount;
		protected TextBox individualPostCount;
		protected TextBox servicePostCountLimit;
		protected PlaceHolder OptionHolder;

		#endregion
	
		override protected void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
            
			SaveButton.Text = ResourceManager.GetString("Save");
			if(!IsPostBack)
			{
				WeblogConfiguration config = WeblogConfiguration.Instance(false);

                if(config.FileOnly)
                {
                    FOStatus.Visible = true;
                    OptionHolder.Visible = false;
                }
	
				enableThemes.Checked = config.EnableThemes;
				aggregatePostSize.Text = config.AggregatePostSize.ToString();
                PopulateDefaultThemes();

				aggregatePostCount.Text = config.AggregatePostCount.ToString();
				individualPostCount.Text = config.IndividualPostCount.ToString();
				servicePostCountLimit.Text = config.ServicePostCountLimit.ToString();

			}
		}


        private void PopulateDefaultThemes()
        {
            defaultTheme.Items.Clear();

            string dirPath = Context.Server.MapPath(string.Concat(SiteUrls.Instance().Locations["Themes"], "/Blogs"));

            DirectoryInfo dirAllThemes = new DirectoryInfo(dirPath);
            foreach (DirectoryInfo dirTheme in dirAllThemes.GetDirectories())
            {
                DirectoryInfo dirStyle = new DirectoryInfo(Path.Combine(dirTheme.FullName, "Style"));
                if (dirStyle.Exists)
                {
                    defaultTheme.Items.Add(new ListItem(dirTheme.Name, dirTheme.Name.ToLower() + "@"));
                    foreach (FileInfo fi in dirStyle.GetFiles("*.css"))
                    {
                        if (string.Compare(fi.Name, "print.css", true) != 0)
                        {
                            string name = Path.GetFileNameWithoutExtension(fi.FullName).ToLower();
                            if (name != "style")
                            {
                                string themeText = string.Format("{0} - {1}", dirTheme.Name, name);
                                string themeValue = string.Format("{0}@{1}.css", dirTheme.Name, name);
                                defaultTheme.Items.Add(new ListItem(themeText, themeValue.ToLower()));
                            }
                        }
                    }

                }
            }

            string theme = string.Format("{0}@{1}", WeblogConfiguration.Instance().DefaultTheme, WeblogConfiguration.Instance().DefaultSecondaryCSS).ToLower().Trim();

            ListItem liTheme = defaultTheme.Items.FindByValue(theme);
            if (liTheme != null)
                liTheme.Selected = true;

        }


        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;

            WeblogConfiguration config = WeblogConfiguration.Instance(false);

            if (config.FileOnly)
                return;

            config.EnableThemes = this.enableThemes.Checked;

			config.AggregatePostCount = Int32.Parse(aggregatePostCount.Text);
			config.IndividualPostCount = Int32.Parse(individualPostCount.Text);
			config.ServicePostCountLimit = Int32.Parse(servicePostCountLimit.Text);
			config.AggregatePostSize = Int32.Parse(aggregatePostSize.Text);

            string[] theme = defaultTheme.SelectedValue.Split('@');
            config.DefaultTheme = theme[0];
            if (theme.Length > 1)
                config.DefaultSecondaryCSS = theme[1];
            else
                config.DefaultSecondaryCSS = null;
            
            config.Save();

            CSCache.Remove(ThemeManager.CacheKey);

            Status.Visible = true;
        }

	}
}
